<template>
	<view class="all-main">
		<view class="backstage_logo">
			<image class="Max" src="../../static/app.png" mode=""></image>
		</view>
		<view class="backstage_put">
			<text class="msg">您将为【{{name}}】账号绑定微信，请点击下方确认按钮！</text>
		</view>
		<view class="backstage_login" @click="btnSave">
			确认
		</view>
	</view>
</template>

<script>
	var util = require('@/util/request5.js');
	export default {
		data() {
			return {
				admin_id: '',
				group_id: '',
				name: '',
				openid: '',
				is_true: true
			}
		},
		onLoad(e) {
			this.admin_id = e.admin_id
			this.group_id = e.group_id
			this.name = e.name
			this.openid = uni.getStorageSync('openid');
			if (this.openid == '' || this.openid == undefined) {
				if (e.code) {
					this.getWxOpenId(e.code)
				} else {
					this.getWxConfigInfo(e.code)
				}
			}
		},
		methods: {
			// 获取微信公众号信息
			async getWxConfigInfo() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Wechats/getWxConfigInfo'
				});
				if (res.code == 1) {
					uni.setStorageSync('wxInfo', res.data);
					this.getCodes('')
				}
			},
			// 获取CODE
			getCodes(code) {
				if (code == '' || code == undefined) {
					this.wxInfo = uni.getStorageSync('wxInfo');
					const redirect_url = util.WEB_URL + '/pages/public/customerbind?admin_id=' + this.admin_id + '&group_id=' + this.group_id + '&name=' + this.name;
					const url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.wxInfo.AppID +
						'&redirect_uri=' + encodeURIComponent(redirect_url) +
						'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
					window.location.href = url
				} else {
					this.getWxOpenId(code)
				}
			},
			// 获取微信OPENID
			async getWxOpenId(code) {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Wechats/getWxOpenId',
					data: {
						code: code
					}
				});
				if (res.code == 1) {
					if (res.data != null && res.data != '' && res.data != undefined) {
						this.openid = res.data
						uni.setStorageSync('openid', res.data);
					}
				}
			},
			// 绑定微信
			async btnSave() {
				if (this.is_true) {
					this.is_true = false
					const {
						data: res
					} = await this.$myHttp5({
						url: 'Register/customer_binds_wx',
						data: {
							openid: this.openid,
							gid: this.group_id,
							aid: this.admin_id
						}
					});
					uni.showToast({
						title: res.msg,
						icon: "none"
					});
					if (res.code == 1) {
						uni.setStorageSync('userinfo', {'id': 1});
						uni.setStorageSync('userType', 3);
						setTimeout(function() {
							var ua = window.navigator.userAgent.toLowerCase();
							if (ua.match(/MicroMessenger/i) == 'micromessenger') {
							    WeixinJSBridge.call('closeWindow');
							} else {
							    window.close();
							}
							// uni.navigateTo({
							// 	url: '/pages/customer/login'
							// })
						}, 1500)
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.all-main {
		.backstage_logo {
			display: flex;
			justify-content: center;
		
			image {
				width: 200rpx;
				height: 200rpx;
				margin-top: 100rpx;
				margin-bottom: 60rpx;
			}
		}
		
		.backstage_put {
			border-bottom: #373b54 1px solid;
			padding: 30rpx;
			box-sizing: border-box;
			text-align: center;
			width: 90%;
			margin: auto;
		
			text {
				font-size: 28rpx;
				color: #333;
			}
		}
		
		.backstage_login {
			width: 90%;
			margin: 160rpx auto 0 auto;
			background-color: #2e3e55;
			color: #fff;
			font-size: 28rpx;
			text-align: center;
			padding: 20rpx 0;
			border-radius: 15rpx;
			transition: all 0.4s;
		}
		
		.backstage_login:active {
			opacity: 0.8;
		}
	}
</style>
